/* eslint-disable no-nested-ternary */
/* eslint-disable array-callback-return */
export const PAGE_CODE = 'dcs-parts-partOrderSatisfyRate';
import moment from 'moment';
import {formatMessage} from './intl';
import {formatAmount} from '../utils';
export const DEFAULT_QUERY_OPTION = {
    dealerId: [],
    type: 1,
    month: moment().format('YYYY-MM')
};

const TotalSum = (text, index, data, type, prop) => {
    if((index + 1) === data.length) {
        let value = 0;
        data.map(x => {
            if(x[type] !== undefined)
                value += x[type];
        });
        return {
            children: value !== null && (prop ? value : formatAmount(value))
        };
    }
    return {
        children: text !== null && (prop ? text : formatAmount(text))
    };
};

export const columns = (conditions, data) => {
    const datalist = [
        {
            title: formatMessage({
                id: 'tablePanel.column.month',
                defaultMessage: '月份'
            }),
            dataIndex: 'month'
        },
        {
            title: formatMessage({
                id: 'tablePanel.column.orderCount',
                defaultMessage: '订单份数'
            }),
            dataIndex: 'orderCount',
            render: (text, row, index) => TotalSum(text, index, data, 'orderCount')
        }
    ];
    if(conditions.type === 1)
        datalist.push({
            title: formatMessage({
                id: 'tablePanel.column.piVarietyQty',
                defaultMessage: 'PI项数'
            }),
            dataIndex: 'piVarietyQty',
            render: (text, row, index) => TotalSum(text, index, data, 'piVarietyQty')
        }, {
            title: formatMessage({
                id: 'tablePanel.column.deliveryVarietyQty',
                defaultMessage: '备货项数'
            }),
            dataIndex: 'deliveryVarietyQty',
            render: (text, row, index) => TotalSum(text, index, data, 'deliveryVarietyQty')
        }, {
            title: formatMessage({
                id: 'tablePanel.column.varietyMeetRate',
                defaultMessage: '品种满足率'
            }),
            dataIndex: 'varietyMeetRate',
            render: (text, row, index) => {
                if(index === data.length - 1) {
                    const x = TotalSum(row.piVarietyQty, index, data, 'piVarietyQty', '%').children;
                    const y = TotalSum(row.deliveryVarietyQty, index, data, 'deliveryVarietyQty', '%').children;
                    return {
                        children: `${(formatAmount((y / x)) * 100).toFixed(2)}%`
                    };
                }
                return {
                    children: `${TotalSum(text, index, data, 'varietyMeetRate').children}%`
                };
            }
        });
    else if(conditions.type === 2)
        datalist.push({
            title: formatMessage({
                id: 'tablePanel.column.piAmount',
                defaultMessage: 'PI金额'
            }),
            dataIndex: 'piAmount',
            render: (text, row, index) => TotalSum(text, index, data, 'piAmount')
        }, {
            title: formatMessage({
                id: 'tablePanel.column.deliveryAmount',
                defaultMessage: '备货金额'
            }),
            dataIndex: 'deliveryAmount',
            render: (text, row, index) => TotalSum(text, index, data, 'deliveryAmount')
        }, {
            title: formatMessage({
                id: 'tablePanel.column.amountMeetRate',
                defaultMessage: '金额满足率'
            }),
            dataIndex: 'amountMeetRate',
            render: (text, row, index) => {
                if(index === data.length - 1) {
                    const x = TotalSum(row.piAmount, index, data, 'piAmount', '%').children;
                    const y = TotalSum(row.deliveryAmount, index, data, 'deliveryAmount', '%').children;
                    return {
                        children: `${(formatAmount((y / x)) * 100).toFixed(2)}%`
                    };
                }
                return {
                    children: `${TotalSum(text, index, data, 'amountMeetRate').children}%`
                };
            }
        });
    else
        datalist.push({
            title: formatMessage({
                id: 'tablePanel.column.piQuantity',
                defaultMessage: 'PI数量'
            }),
            dataIndex: 'piQuantity',
            render: (text, row, index) => TotalSum(text, index, data, 'piQuantity')
        }, {
            title: formatMessage({
                id: 'tablePanel.column.deliveryQuantity',
                defaultMessage: '备货数量'
            }),
            dataIndex: 'deliveryQuantity',
            render: (text, row, index) => TotalSum(text, index, data, 'deliveryQuantity')
        }, {
            title: formatMessage({
                id: 'tablePanel.column.quantityMeetRate',
                defaultMessage: '数量满足率'
            }),
            dataIndex: 'quantityMeetRate',
            render: (text, row, index) => {
                if(index === data.length - 1) {
                    const x = TotalSum(row.piQuantity, index, data, 'piQuantity', '%').children;
                    const y = TotalSum(row.deliveryQuantity, index, data, 'deliveryQuantity', '%').children;
                    return {
                        children: `${(formatAmount((y / x)) * 100).toFixed(2)}%`
                    };
                }
                return {
                    children: `${TotalSum(text, index, data, 'quantityMeetRate').children}%`
                };
            }
        });
    return datalist;
};
